package com.example.meitu.api.util;

import cn.hutool.json.JSONException;
import cn.hutool.json.JSONObject;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.filter.HTTPBasicAuthFilter;
import com.sun.jersey.core.util.MultivaluedMapImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import javax.ws.rs.core.MediaType;
import java.util.logging.Level;
import java.util.logging.Logger;
@Slf4j
@Component
public class SmsUtils {
    @Value("${sms.username}")
    public String username;
    @Value("${sms.password}")
    public static final String password = "key-6150c3dd107c05d5373c6aa96b50662f";

    @Value("${sms.sign}")
    public static final String SIGN = "【DBRank】";
    @Async
    public String sendMsg(String phone, String msg){
        // just replace key here
        Client client = Client.create();
        client.addFilter(new HTTPBasicAuthFilter(
                username,password));
        WebResource webResource = client.resource(
                "http://sms-api.luosimao.com/v1/send.json");
        MultivaluedMapImpl formData = new MultivaluedMapImpl();
        formData.add("mobile", phone);
        formData.add("message", msg + SIGN);
        ClientResponse response =  webResource.type( MediaType.APPLICATION_FORM_URLENCODED ).
                post(ClientResponse.class, formData);
        String textEntity = response.getEntity(String.class);
        log.info("sendMsg: phone:{}, msg:{}, textEntity:{}", phone, msg, textEntity);
        return textEntity;
    }

    public String testStatus(){
        Client client = Client.create();
        client.addFilter(new HTTPBasicAuthFilter(
                username,password));
        WebResource webResource = client.resource( "http://sms-api.luosimao.com/v1/status.json" );
        MultivaluedMapImpl formData = new MultivaluedMapImpl();
        ClientResponse response =  webResource.get( ClientResponse.class );
        String textEntity = response.getEntity(String.class);
        log.info("testStatus: textEntity:{}", textEntity);
        return textEntity;
    }

    public static void main(String[] args) {

    }
}
